package com.thinkbig.segment.resource;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class ResourceLoader {
    public static void loadDict(String fileName) {
        try {
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            String line = null;
            System.out.println("Begin to load dict file...");
            int wordNum = 0;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.startsWith("#")) {
                    continue;
                }
                String[] segs = line.split("\\t");
                if (segs.length != 2) {
                    continue;
                }
                int key = segs[0].length();
                String word = segs[0];
                double score = Double.parseDouble(segs[1]);

                if (!Resourcer.DICT.containsKey(segs[0].length())) {
                    Resourcer.DICT.put(new Integer(key), new HashMap<String, Double>());
                }
                Resourcer.DICT.get(key).put(word, score);
                wordNum++;
            }
            br.close();
            fr.close();
            System.out.println(String.format("%d words have been loaded from dict!", wordNum));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadFavicon(String faviconFile) throws IOException {
        FileInputStream fis = new FileInputStream(faviconFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] tmp = new byte[bis.available()];
        
        while (bis.read(tmp) != -1) {
            continue;
        }
        bis.close();
        fis.close();
        Resourcer.FAVICON = tmp;
    }

}
